home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Educ / Calc / MathPad 2.35.sit / XFuns / XFun kit / cross.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-23  |  1.8 KB  |  66 lines  |  [TEXT/KAHL]

  1. /* example XFun that processes index values. Computes a 3D cross product
  2.  
  3. MathPad will call the function cross() 3 times to access 3 vector components.
  4. An "array" function must compute its return value based on the index.
  5. MathPad only evaluates the elements that it needs and there are no guarantees
  6. as to which elements will be evaluated or what order they are evaluated in.
  7.  
  8. For computing the cross product, using GetExprMatrix() to get the input vectors
  9. is simple but is not the most efficient technique since it will evaluate all
  10. elements of both vectors on each call.
  11. */
  12.  
  13. #include "callback.h"
  14.  
  15. BOOL cross(extended *retval,funptr callback)
  16. {
  17.    EXPR xpr;
  18.    extended *a,*b,i;
  19.    long rows,cols;
  20.    BOOL ok;
  21.    
  22.    ok = TRUE;
  23.    if(!GetParmVal(2,&i,callback)) return(FALSE);    /* index value is parm 2 */
  24.    
  25.    MakeParmExpr(1,&xpr,callback);                    /* vector A is parm 1 */
  26.    if(!GetExprMatrix(xpr,&a,&rows,&cols,callback)) ok = FALSE;
  27.    FreeExpr(xpr,callback);
  28.    if(!ok || rows !=3 || cols !=0)
  29.    {
  30.     DisposPtr(a);
  31.     return(FALSE);
  32.    }
  33.    MakeParmExpr(0,&xpr,callback);                    /* vector B is parm 0 */
  34.    if(!GetExprMatrix(xpr,&b,&rows,&cols,callback)) ok = FALSE;
  35.    FreeExpr(xpr,callback);
  36.    if(!ok || rows !=3 || cols !=0)
  37.    {
  38.     DisposPtr(a);
  39.     DisposPtr(b);
  40.     return(FALSE);
  41.    }
  42.   
  43.    switch((int)i)            /* calculate component for a given index value */
  44.    {
  45.     case 1: *retval = a[1] * b[2] - a[2] * b[1]; break;
  46.     case 2: *retval = a[2] * b[0] - a[0] * b[2]; break;
  47.     case 3: *retval = a[0] * b[1] - a[1] * b[0]; break;
  48.     default: ok = FALSE;
  49.    }
  50.    
  51.    DisposPtr(a);
  52.    DisposPtr(b);
  53.    return(ok);
  54. }
  55.  
  56. void predef(funptr callback)
  57. {
  58.    AddFunDim("cross",3,callback);  /* cross(A,B)[i] = xfun(i,A,B) dim[3] */
  59. }
  60.  
  61. void main(funptr callback)
  62. {
  63.    AddXfun("cross","A,B",&cross,&predef,callback);
  64. }
  65.  
  66.